package dao;

import model.Favorite;
import model.Goods;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import utils.DataSourceUtils;

import java.sql.SQLException;
import java.util.List;

/**
 * 收藏功能的数据访问对象
 * 负责处理与收藏相关的数据库操作
 */
public class FavoriteDao {
    
    /**
     * 添加收藏
     * @param favorite 收藏对象
     * @return 是否添加成功
     * @throws SQLException SQL异常
     */
    public boolean addFavorite(Favorite favorite) throws SQLException {
        QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "insert into favorite(user_id, goods_id, create_time) values(?, ?, ?)";
        int result = r.update(sql, favorite.getUser_id(), favorite.getGoods_id(), favorite.getCreate_time());
        return result > 0;
    }
    
    /**
     * 删除收藏
     * @param userId 用户ID
     * @param goodsId 商品ID
     * @return 是否删除成功
     * @throws SQLException SQL异常
     */
    public boolean deleteFavorite(int userId, int goodsId) throws SQLException {
        QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "delete from favorite where user_id = ? and goods_id = ?";
        int result = r.update(sql, userId, goodsId);
        return result > 0;
    }
    
    /**
     * 检查商品是否已被用户收藏
     * @param userId 用户ID
     * @param goodsId 商品ID
     * @return 是否已收藏
     * @throws SQLException SQL异常
     */
    public boolean isFavorite(int userId, int goodsId) throws SQLException {
        QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "select * from favorite where user_id = ? and goods_id = ?";
        Favorite favorite = r.query(sql, new BeanHandler<Favorite>(Favorite.class), userId, goodsId);
        return favorite != null;
    }
    
    /**
     * 获取用户收藏的商品列表
     * @param userId 用户ID
     * @param pageNumber 页码
     * @param pageSize 每页显示数量
     * @return 收藏的商品列表
     * @throws SQLException SQL异常
     */
    public List<Goods> getFavoriteGoods(int userId, int pageNumber, int pageSize) throws SQLException {
        QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "select g.id, g.name, g.cover, g.image1, g.image2, g.price, g.intro, g.stock, t.id typeid, t.name typename " +
                "from goods g, type t, favorite f " +
                "where f.user_id = ? and f.goods_id = g.id and g.type_id = t.id " +
                "order by f.create_time desc limit ?, ?";
        return r.query(sql, new BeanListHandler<Goods>(Goods.class), userId, (pageNumber - 1) * pageSize, pageSize);
    }
    
    /**
     * 获取用户收藏商品的总数
     * @param userId 用户ID
     * @return 收藏商品总数
     * @throws SQLException SQL异常
     */
    public int getFavoriteCount(int userId) throws SQLException {
        QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "select count(*) from favorite where user_id = ?";
        return r.query(sql, new ScalarHandler<Long>(), userId).intValue();
    }
    
    /**
     * 获取用户收藏商品的总数（与getFavoriteCount方法功能相同，为保持接口一致）
     * @param userId 用户ID
     * @return 收藏商品总数
     * @throws SQLException SQL异常
     */
    public int getFavoriteCountByUserId(int userId) throws SQLException {
        return getFavoriteCount(userId);
    }
    
    /**
     * 获取用户收藏的商品列表（与getFavoriteGoods方法功能相同，为保持接口一致）
     * @param userId 用户ID
     * @param pageNumber 页码
     * @param pageSize 每页显示数量
     * @return 收藏的商品列表
     * @throws SQLException SQL异常
     */
    public List<Goods> getFavoriteGoodsByUserId(int userId, int pageNumber, int pageSize) throws SQLException {
        return getFavoriteGoods(userId, pageNumber, pageSize);
    }
}